<template>
    <div class="loginForm">
        <el-form :rules="rules" :model="loginData" status-icon ref="loginForm">
            <h3>系统登录</h3>
            <el-form-item prop="username">
                <el-input prefix-icon="el-icon-user" placeholder="请输入用户名" size="small" type="text" v-model="loginData.username"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input prefix-icon="el-icon-lock" placeholder="请输入用户密码" size="small" type="password" v-model="loginData.password"></el-input>
            </el-form-item>
            <el-form-item>
                <el-checkbox v-model="rememberMe">记住密码</el-checkbox>
            </el-form-item>
            <el-button class="loginBtn" size="small" type="primary" @click="submitLogin">登录</el-button>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                rememberMe : true,
                loginData:{
                    username : 'admin',
                    password : '123'
                },
                rules : {
                    username : [{required : true,message:'请正确填写用户名称',trigger:'blur'}],
                    password : [{required : true,message:'请正确填写用户密码',trigger:'blur'}]
                },
            }
        },
        methods: {
            // 提交登录
            submitLogin() {
                this.$refs['loginForm'].validate((valid) => {
                    if (valid) {
                        // 发送登录请求到 /doLogin
                        this.$postKeyValueRequest('/doLogin',this.loginData).then(resp => {
                            if (resp) {
                                // 把 登录者信息 存入了 浏览器 session
                                window.sessionStorage.setItem('loginer', JSON.stringify(resp.data));
                                // 跳转Home页
                                this.$router.replace('/home');
                            }
                        })
                    } else {
                        this.$message.warning('请按照要求填写登录信息');
                        return false;
                    }
                });
            }
        }
    }
</script>

<style scoped>
    .loginForm{
        width: 400px;
        padding:20px;
        margin:180px auto;
    }
    .loginForm h3{
        text-align: center;
        color: #fff;
    }
    .loginForm .loginBtn{
        width: 100%;
    }
</style>